<template>
  <view v-show="goodsInfo.goods_name">
    <!-- 轮播图 -->
    <swiper class="swiper" indicator-dots autoplay :interval="3000" :duration="1000" circular @click="preview">
      <block v-if="goodsInfo.pics && goodsInfo.pics.length > 0">
        <swiper-item class="item" v-for="(it,i) in goodsInfo.pics" :key="i">
          <image class="s_img" :data-i="i" :src="it.pics_big" mode=""></image>
        </swiper-item>
      </block>
    </swiper>
    
    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="price">￥{{goodsInfo.goods_price}}</view>
      <!-- 信息主体区域 -->
      <view class="goods-info-body">
        <!-- 商品名称 -->
        <view class="goods-name">{{goodsInfo.goods_name}}</view>
        <!-- 收藏 -->
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="yf">快递：免运费</view>
    </view>
    
    <!-- 富文本 -->
    <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
    
    <!-- 购物车 -->
    <view class="good-nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick"></uni-goods-nav>
    </view>
  </view>
</template>

<script>
  import {getGoodsDetail} from '../../utils/api.js'
  import {mapActions,mapGetters} from 'vuex'
  export default {
    data() {
      return {
        goodsInfo:{},
        // 左侧按钮组的配置对象
        options:[
          {
            icon:'shop',
            text:'商铺'
          },
          {
            icon:'cart',
            text:'购物车',
            info:2
          }
        ],
        // 右侧按钮组的配置对象
        buttonGroup:[
          {
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    onLoad(opts) {
      if(opts.goods_id !== undefined) {
      this.getGoodsDetail(opts)
      }
    },
    methods:{
      // store映射方法
      ...mapActions('cart',['addGood']),
      // 获取商品列表
     async getGoodsDetail(params){
       try{
         const {data:{message}} = await getGoodsDetail(params)
         // 去除图片间隙
         message.goods_introduce = message.goods_introduce.replace(/<img /g,"<img style='display:block;'").replace(/webp/g,"jpg")
         this.goodsInfo = message
       }catch(e){
         //TODO handle the exception
       }
     },
     // 预览图片
     preview(e) {
       // console.log(e.target);
       const i = e.target.dataset.i
       if(i !== undefined) {
         // 调用小程序预览图片方法
         uni.previewImage({
           // 预览图的索引
           current:i,
           // 预览图片列表，每一项是图片地址
           urls:this.previewList
         })
       }
     },
     // 购物车左侧按钮点击事件
     onClick(e){
      // console.log(e);
      if(e.content.text === '购物车'){
        uni.switchTab({
          url:'/pages/cart/cart'
        })
      }
     },
    // 购物车右侧按钮点击事件
     buttonClick(e){
       // console.log(e);
       if(e.content.text === "加入购物车") {
         this.addGood(this.goodsInfo)
       }
     }
   },
   computed:{
     // store映射数据
     ...mapGetters('cart',['goodsTotalNum']),
     previewList(){
       const pics = this.goodsInfo.pics
       if(!pics) return []
       return pics.map(pic => pic.pics_big)
     }
   },
   watch:{
     goodsTotalNum:{
       immediate:true,
       handler(newVal){
         this.options[1].info = newVal
       }
     }
   }
  }
</script>

<style lang="scss">
  // 轮播图
.swiper {
  height: 750rpx;
  .item {
    height: 100%;
    .s_img {
      width: 100%;
      height: 100%;
    }
  }
}
// 商品信息区域的样式
.goods-info-box {
  padding: 10px;
  padding-right: 0;

  .price {
    color: #c00000;
    font-size: 18px;
    margin: 10px 0;
  }

  .goods-info-body {
    display: flex;
    justify-content: space-between;

    .goods-name {
      font-size: 13px;
      padding-right: 10px;
    }
    // 收藏区域
    .favi {
      width: 120px;
      font-size: 12px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      border-left: 1px solid #efefef;
      color: gray;
    }
  }

  // 运费
  .yf {
    margin: 10px 0;
    font-size: 12px;
    color: gray;
  }
}

// 购物车
.good-nav {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
</style>
